<?php if (!defined("BASEPATH")) exit("No direct script access allowed"); ?>

<script type="text/javascript">
	Ext.onReady(function() {

		var schStore = new Ext.data.JsonStore({
			totalProperty: 'total',
			root: 'results',
			sortInfo: {field: 'schName', direction: 'ASC'},
			remoteSort: true,
			proxy : new Ext.data.HttpProxy({
				url: '<?=site_url()?>admin/get_scholarship',
				method: 'GET'
			}),
			fields: [
				{name: 'schId'},
				{name: 'schName'},
				{name: 'schSubId'},
				{name: 'schSubName'},
				{name: 'schUrlName'},
				{name: 'schListDate'},
				{name: 'e2cTempFlag'},
				{name: 'insList'},
				{name: 'couList'},
				{name: 'majList'},
				{name: 'schPresenter'}
			]
		});
		
		new Ext.grid.GridPanel({
			store: schStore,
			id: 'schGrid',
			viewConfig:{
				emptyText: '<?=$this->db->escape_str($this->lang->line("THERE_IS_CURRENTLY_NO_DATA_TO_DISPLAY"))?>',
				getRowClass: function(record, index) {
					if(record.data.e2cTempFlag == 'I') return 'temp_data_exists';
					else if(record.data.schPresenter == 'I') return 'presenter_live';
					else return '';
				}
			},
			columns: [
				{id: 'schName', header: '<?=$this->db->escape_str($this->lang->line("SCHOLARSHIP_NAME"))?>', width: 200, sortable: true, dataIndex: 'schName'}
			],
			stripeRows: true,
			autoExpandColumn: 'schName',
			autoHeight: true,
			autoWidth: true,
			sm: new Ext.grid.RowSelectionModel({singleSelect:true}),

			listeners: {

				rowdblclick: function(grid, rowIndex, event) {
					if(rowIndex != 'new') {
						var record = grid.getStore().getAt(rowIndex);
					} else {
						var record = new Object();
						record.data = new Object();
					}
					DataEdit.showWindow('SCH', ((rowIndex != 'new')?(''):('-')) + record.data.schId)
				},

				activate: function() {
					schStore.load({params:{start:0, limit:10}});
				}

			},
			border: false,
			frame: false,

			tbar: {
				items: [{
						icon: '<?=MEDIA?>icon/application_form_add.png',
						text: '<?=$this->db->escape_str($this->lang->line("ADD_NEW"))?>',
						handler: function(){
							DataEdit.showWindow('SCH', -1);
						}
					}, {
							xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_edit.png',
						text: '<?=$this->db->escape_str($this->lang->line("EDIT"))?>',
						ref: '../editBtn',
						disabled: true,
						handler: function() {
							Ext.getCmp('schGrid').fireEvent('rowdblclick', Ext.getCmp('schGrid'), Ext.getCmp('schGrid').store.indexOf(Ext.getCmp('schGrid').getSelectionModel().getSelected()), new Object());
						}
					}, {
							xtype: 'tbseparator'
					}, {
						icon: '<?=MEDIA?>icon/application_form_delete.png',
						text: '<?=$this->db->escape_str($this->lang->line("DELETE"))?>',
						ref: '../removeBtn',
						disabled: true,
						handler: function() {
							DataEdit.delete('SCH', Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schId, Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schName);
						}
					}, {
					xtype: 'tbseparator'
				}, {
					icon: '<?=MEDIA?>icon/application_form_preview.png',
					text: '<?=$this->db->escape_str($this->lang->line("PREVIEW"))?>',
					ref: '../previewBtn',
					disabled: true,
					handler: function(){
						window.open('<?=site_url()?>preview/' + '/scholarships/' + Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schUrlName);
					}
				}, {
					xtype: 'tbseparator'
				}, {
					icon: '<?=MEDIA?>icon/application_form_publish.png',
					text: '<?=$this->db->escape_str($this->lang->line("PUBLICATION"))?>',
					ref: '../publishBtn',
					disabled: true,
					handler: function(){
						DataEdit.publish('SCH', Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schId, Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schName, 1);
					}
				}, {
					xtype: 'tbseparator'
				}, {
					icon: '<?=MEDIA?>icon/application_form_publish_delete.png',
					text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_UNPUBLISHED_DATA"))?>',
					ref: '../publishBtnDelete',
					disabled: true,
					handler: function(){
						DataEdit.publish('SCH', Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schId, Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schName, 0);
					}
				}, {
					xtype: 'tbseparator'
				}, {
					icon: '<?=MEDIA?>icon/application_form_publish_remove.png',
					text: '<?=$this->db->escape_str($this->lang->line("CANCELLING_OF_PUBLISHED_DATA"))?>',
					ref: '../unpublishBtn',
					disabled: true,
					handler: function(){
						DataEdit.unpublish('SCH', Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schId, Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schName, 2);
					}
				}, {
					xtype: 'tbseparator'
				}, {
					icon: '<?=MEDIA?>icon/image.png',
					text: '<?=$this->db->escape_str($this->lang->line("MANAGING_IMAGES"))?>',
					ref: '../imageBtn',
					disabled: true,
					handler: function(){
						DataEdit.image('SCH', Ext.getCmp('schGrid').getSelectionModel().getSelections()[0].data.schId);
					}
				}
				]
			},
			
			bbar:
				new Ext.PagingToolbar({
					pageSize: <?=$this->config->item("adminGridPageSize")?>,
					store: schStore,
					displayInfo: false,
					plugins: new Ext.ux.ProgressBarPager()
				}),			

			plugins:[
				new Ext.ux.grid.Search({
					minChars:2,
					iconCls: 'icon-zoom',
					searchText:'<?=$this->db->escape_str($this->lang->line("SEARCH"))?>',
					position:'top',
					minCharsTipText:'<?=$this->db->escape_str($this->lang->line("MINIMUM_TWO_CHARACTERS_IS_REQUIRED"))?>',
					selectAllText: '<?=$this->db->escape_str($this->lang->line("SELECT_ALL"))?>',
					width:150,
					disableIndexes:[],
					autoFocus:true
				})]

		})

		Ext.getCmp('schGrid').getSelectionModel().on('selectionchange', function(sm){
			Ext.getCmp('schGrid').removeBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('schGrid').editBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('schGrid').publishBtn.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('schGrid').publishBtnDelete.setDisabled(!(sm.getCount() && sm.getSelected().data.e2cTempFlag == 'I'));
			Ext.getCmp('schGrid').unpublishBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('schGrid').previewBtn.setDisabled(sm.getCount() < 1);
			Ext.getCmp('schGrid').imageBtn.setDisabled(sm.getCount() < 1);
		});

		Ext.getCmp('schGrid').render('grid-table-sch');
		schStore.load({params:{start:0, limit:<?=$this->config->item("adminGridPageSize")?>}});
	})
</script>


<div id="grid-table-sch"></div>